﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using GCC.APIContainer.Utilities;
using GCC.Container.Abstracts;
using GCC.Container.Models;

using Microsoft.AspNetCore.Mvc;

using tcm.entity.System;
using tcm.utils;
using tcm.request.System;
using tcm.Filters;

namespace tcm.Controller.SystemManage
{
    [ApiController]//记住这个单词
    [Route("tcm/sysnotice")]//记住这个
    public class SysNoticeController : GCCAPIControllerBase
    {
        private readonly AbsGCCContext _context;
        public SysNoticeController(AbsGCCContext context) : base(context)
        {
            _context = context;
        }

        /// <summary>
        /// 查询通知公告表列表
        /// </summary>
        /// <returns></returns>
        [HttpGet("getlist")]
        [TCMAuth(permission: "system:notice:list")]
        public DataResponse<List<SysNotice>> GetList([FromQuery] SysNoticeRequest request)
        {
            var dbres = GCCEntrance.DBManager!.LocalDB!.Select<SysNotice>()
               .WhereIf(!string.IsNullOrWhiteSpace(request.notice_title), item => item.notice_title.Contains(request.notice_title!))
               .WhereIf(request.status.HasValue, item => item.status==request.status.Value!)
               .Count(out var count)
               .Page(request.PageIndex ?? 1, request.PageSize)
               .OrderByDescending(x=>x.create_date)
               .ToList();

            return HandleData(dbres, count);
        }

        /// <summary>
        /// 查询通知公告表详情
        /// </summary>
        /// <returns></returns>
        [HttpGet("get/{id}")]
        public DataResponse<SysNotice> GetSysNotice([FromRoute] long id)
        {
            var res = GCCEntrance.DBManager!.LocalDB!.Queryable<SysNotice>().Where(x => x.id==id).First();

            return HandleData(res);
        }


        /// <summary>
        /// 添加通知公告
        /// </summary>
        /// <returns></returns>
        [HttpPost("add")]
        [TCMAuth(permission: "system:notice:add")]
        public DataResponse<bool> AddSysNotice([FromBody] SysNoticeDto request)
        {
            var saveRes = false;

            if (GCCEntrance.DBManager!.LocalDB!.Queryable<SysNotice>().Where(x => x.notice_title==request.notice_title).Any())
            {
                return SystemError<bool>("公告标题已存在");
            };

            GCCEntrance.DBManager!.LocalDB!.Transaction(() =>
            {
                _=request.Create(_context, HttpContext.GetToken());
                var info = new SysNotice()
                {
                    id=request.id,
                    notice_title=request.notice_title,
                    status=request.status,
                    notice_content=request.notice_content,
                    notice_type=request.notice_type,
                    remark=request.remark,
                    create_date=request.create_date,
                    create_by=request.create_by,
                    update_by=request.update_by,
                    update_date=request.update_date
                };

                if (GCCEntrance.DBManager!.LocalDB!.Insert(info).ExecuteAffrows() != 1)
                {
                    throw new Exception();
                }
                saveRes=true;
            });

            return HandleData(saveRes);
        }

        /// <summary>
        /// 更新通知公告
        /// </summary>
        /// <returns></returns>
        [HttpPost("update")]
        [TCMAuth(permission: "system:notice:update")]
        public DataResponse<bool> UpdateSysNotice([FromBody] SysNoticeDto request)
        {
            var saveRes = false;
            if (request.id<=0)
                return SystemError<bool>("id为空");

            if (GCCEntrance.DBManager!.LocalDB!.Queryable<SysNotice>().Where(x => x.notice_title==request.notice_title&&x.id !=request.id).Any())
            {
                return SystemError<bool>("公告标题已存在");
            };

            GCCEntrance.DBManager!.LocalDB!.Transaction(() =>
            {
                _=request.Modify(_context, HttpContext.GetToken());
                saveRes = GCCEntrance.DBManager!.LocalDB!.Update<SysNotice>(request.id)
                .Set(item => item.notice_title, request.notice_title)
                .Set(item => item.status, request.status)
                .Set(item => item.notice_content, request.notice_content)
                .Set(item => item.notice_type, request.notice_type)
                .Set(item => item.remark, request.remark)
                .Set(item => item.update_by, request.update_by)
                .Set(item => item.update_date, request.update_date)
                .ExecuteAffrows()==1;
            });

            return HandleData(saveRes);
        }

        /// <summary>
        /// 删除公告
        /// </summary>
        /// <returns></returns>
        [HttpDelete("delete/{ids}")]
        [TCMAuth(permission: "system:notice:remove")]
        public DataResponse<bool> DeleteSysNotice([FromRoute] string ids)
        {
            var saveRes = false;
            if (string.IsNullOrWhiteSpace(ids))
                return SystemError<bool>("id为空");

            var idList = SysHelper.SpitIntArrary(ids);

            saveRes = GCCEntrance.DBManager!.LocalDB!.Delete<SysNotice>().Where(x => idList.Contains(x.id)).ExecuteAffrows()>0;

            return HandleData(saveRes);
        }

    }
}
